package com.example.filelock;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.filelock.activity.SetActivity;
import com.example.filelock.fragment.DecryptionFragment;
import com.example.filelock.fragment.EncryptionFragment;
import com.example.filelock.utils.BasDataUtil;
import com.example.filelock.utils.FileUtil;
import com.example.filelock.utils.PageViewUitl;
import com.google.android.material.tabs.TabLayout;

import java.security.Key;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class MainActivity extends AppCompatActivity {

    private Toolbar mMainToolbar;
    public static ViewPager mMainLayoutVp;
    private List<String> titles;
    private List<Fragment> fragments;
    private TabLayout mMainLayoutTab;
    private ImageView mMainIvSet;
    public static  int MAIN_RETURN = 12;
    private long firstTime = 0;
    private SharedPreferences sp;
    private  SharedPreferences.Editor editor;


    @Override
    protected void onDestroy() {
        new Thread() {
            @Override
            public void run() {
                //        初始化文件夹
                FileUtil.initMainFile(getApplicationContext());
//                判断是否开启自动删除临时文件
                if (sp.getBoolean(BasDataUtil.SP_AutoFIle,true)){
                    //消除临时文件
                    FileUtil.deleteTemFile();
                }

            }
        }.start();
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取权限
        PageViewUitl.getPermissions(this);
        //获取初始化状态栏
        PageViewUitl.initView(this);
        initView();
        iniFile();
        setView();
    }

    private void setView() {
        mMainLayoutVp.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });
//        tab与viewpage绑定
        mMainLayoutTab.setupWithViewPager(mMainLayoutVp);
//        点击设置
        mMainIvSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), SetActivity.class);
                startActivityForResult(intent,MAIN_RETURN);
            }
        });
    }

    //    初始化
    private void iniFile() {
        new Thread() {
            @Override
            public void run() {
                //        初始化文件夹
                FileUtil.initMainFile(getApplicationContext());
                //                判断是否开启自动删除临时文件
                if (sp.getBoolean(BasDataUtil.SP_AutoFIle,true)){
                    //消除临时文件
                    FileUtil.deleteTemFile();
                }
            }
        }.start();
    }


    private void initView() {
        mMainToolbar = (Toolbar) findViewById(R.id.main_toolbar);
        mMainLayoutVp = (ViewPager) findViewById(R.id.main_layout_vp);
        titles = new ArrayList<>();
        titles.add("加密");
        titles.add("解密");
        fragments = new ArrayList<>();
        fragments.add(new EncryptionFragment());
        fragments.add(new DecryptionFragment());
        mMainLayoutTab = (TabLayout) findViewById(R.id.main_layout_tab);
        mMainIvSet = (ImageView) findViewById(R.id.main_iv_set);
        sp = getSharedPreferences(BasDataUtil.SP_MAIN,MODE_PRIVATE);
        editor = sp.edit();
    }

    //获取权限监听
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 20:
                if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                        grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
                    //同意了
                    Log.d(BasDataUtil.LOG_TOAST, "主框架页提示：获取权限成功");
                } else {
                    //拒绝权限
                    finish();
                }
                break;
        }
    }
//    返回页面监听

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {

        if (requestCode == MAIN_RETURN) {
            if (resultCode == SetActivity.SET_RETURN){
                //返回退出软件命令
                finish();
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
//    拦截返回键

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
//            再次返回
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) {
                Toast.makeText(MainActivity.this, "再次操作退出程序", Toast.LENGTH_SHORT).show();
                firstTime = secondTime;
                return true;
            } else {
                return super.onKeyDown(keyCode, event);
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
